package xyz.xiangyushui.dao.admin;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;
import xyz.xiangyushui.javaBean.employee.Store;

public class StoreDao extends Dao{
	
	private final int startStoreId=100;
	
	public int addStore(String storeName,String comment){
		int id=nextStoreId();
		db.executeUpdate("insert into store values(?,?,?)", new Object[]{nextStoreId(),storeName,comment});
		return id;
	}
	
	@SuppressWarnings("unchecked")
	public int nextStoreId(){
		int result=startStoreId;
		List<Object> list=db.excuteQuery("select max(storeId) from store ", new Object[]{});
		if(list!=null){
			if(((Map<String,Object>)list.get(0)).get("max(storeId)")!=null){
				result=(int)((Map<String,Object>)list.get(0)).get("max(storeId)")+1;
			}
		}
		return result;
	}
	
	
	public List<Store> getAllStores(){
		List<Store> stores=new LinkedList<>();
		List<Object> list=db.excuteQuery("select storeId,name from store ", new Object[]{});
		if(!list.isEmpty()){
			for(Object o:list){
				Store s=new Store();
				@SuppressWarnings("unchecked")
				Map<String,Object> m=(Map<String, Object>) o;
				s.setStoreId((int) m.get("storeId"));
				s.setStoreName((String) m.get("name"));
				stores.add(s);
			}
		}
		return stores;
	}
	
	public void updateStore(int storeId){
		
	}
	
	public boolean exists(String storeName){
		List<Object> stores=db.excuteQuery("select * from store where name=?", new Object[]{storeName});
		return !stores.isEmpty();
	}
	
}
